package com.android.fragmentdemo.adapters
import android.app.Activity
import android.content.Context
import android.util.Log
import androidx.recyclerview.widget.RecyclerView
import com.android.fragmentdemo.CardItemActivity
import com.android.fragmentdemo.HorizontalCustomLayoutManagerActivity
import com.android.fragmentdemo.R
import com.android.fragmentdemo.ViewPager2UseActivity
import com.hengfoxwolf.commonbase.base.databeans.SlipItem
import com.android.fragmentdemo.databinding.RvCustomItemLayoutBinding
import com.android.fragmentdemo.databinding.RvSlipItemLayoutBinding
import com.android.fragmentdemo.databinding.RvStaggeredItemLayoutBinding
import com.android.fragmentdemo.startActivity
import com.hengfoxwolf.commonbase.base.adapter.BaseRvAdapter
import java.util.Random

class StaggeredLayoutManagerAdapter2(private val activity: Activity) : BaseRvAdapter<String, RvStaggeredItemLayoutBinding>() {

    private val mRandom by lazy {
        Random()
    }

    private val heightMap by lazy {
        mutableMapOf<Int,Int>()
    }

    private val colorArr = intArrayOf(
        R.color.color_da8989,
        R.color.color_ffeb3b,
        R.color.color_ff5722,
        R.color.color_03a9f4,
        R.color.color_673ab7,
        R.color.color_C555D8)

    override fun getLayoutId(): Int = R.layout.rv_staggered_item_layout

    override fun bindData(holder: ViewHolder, dataItem: String, position: Int) {
        holder.binding.tvStaggeredContent.text = dataItem
        holder.binding.rvStaggeredRoot.setBackgroundColor(activity.resources.getColor(colorArr[position % colorArr.size]))
        changeViewRootHeight(holder.binding, position)
    }

    private fun changeViewRootHeight(mBinding: RvStaggeredItemLayoutBinding,position: Int) {
        val layoutParams: RecyclerView.LayoutParams =
            mBinding.rvStaggeredRoot.layoutParams as RecyclerView.LayoutParams
        layoutParams.height = if (heightMap[position] == null) {
            val itemHeight = activity.resources.getDimension(R.dimen.dp_150).toInt() + mRandom.nextInt(200)
            heightMap[position] = itemHeight
            itemHeight
        } else {
            heightMap[position]!!
        }
        mBinding.rvStaggeredRoot.layoutParams = layoutParams
    }
}